package com.yaxi.multipkid.filter;

import android.text.Spanned;

/**
 * @author yaxi
 * @date 2019/1/13
 */
public class IntegerLengthFilter extends BaseFilter {

    private int maxIntegerLength;

    public IntegerLengthFilter(int maxIntegerLength) {
        if (maxIntegerLength < 0) {
            maxIntegerLength = Integer.MAX_VALUE;
        }
        this.maxIntegerLength = maxIntegerLength;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (!contains(dest, DIGITS_POINT)) {
            if (contains(source, DIGITS_POINT)) {
                int sourceIntegerLength = indexOf(source, DIGITS_POINT) - start;
                int beforeInsertDestIntegerLength = dstart;
                if (beforeInsertDestIntegerLength > maxIntegerLength) {
                    return "";
                }
                int afterInsertDestIntegerLength = beforeInsertDestIntegerLength + sourceIntegerLength;
                if (afterInsertDestIntegerLength > maxIntegerLength) {
                    int ignoreTail = afterInsertDestIntegerLength - maxIntegerLength;
                    CharSequence integer = source.subSequence(start, indexOf(source, DIGITS_POINT) - ignoreTail);
                    CharSequence decimal = source.subSequence(indexOf(source, DIGITS_POINT), end);
                    StringBuilder builder = new StringBuilder();
                    builder.append(integer).append(decimal);
                    return builder;
                }
            } else {
                if (dest.length() - (dend - dstart) + end - start > maxIntegerLength) {
                    int ignoreTail = dest.length() - (dend - dstart) + end - start - maxIntegerLength;
                    return source.subSequence(start, end - ignoreTail);
                }
            }
        } else {
            if (indexOf(dest, DIGITS_POINT) < dstart) {
                if (contains(source, DIGITS_POINT)) {
                    return "";
                }
            } else if (indexOf(dest, DIGITS_POINT) > dend) {
                if (contains(source, DIGITS_POINT)) {
                    return "";
                }else {
                    int destIntegers = indexOf(dest,DIGITS_POINT) - (dend - dstart);
                    if (destIntegers + end - start > maxIntegerLength){
                        int ignoreTail = destIntegers + end - start - maxIntegerLength;
                        return source.subSequence(start,end - ignoreTail);
                    }
                }
            } else {
                if (!contains(source.subSequence(start, end),DIGITS_POINT)){

                }
                if (dstart + indexOf(source,DIGITS_POINT) - start > maxIntegerLength){
                    int ignoreTail = dstart + indexOf(source,DIGITS_POINT) - start - maxIntegerLength;
                    CharSequence integer = source.subSequence(start, indexOf(source, DIGITS_POINT) - ignoreTail);
                    CharSequence decimal = source.subSequence(indexOf(source, DIGITS_POINT), end);
                    StringBuilder builder = new StringBuilder();
                    builder.append(integer).append(decimal);
                    return builder;
                }
            }
        }
        return null;
    }
}
